package org.team4.util;

import java.util.HashMap;

import org.team4.constants.StringConstant;
import org.team4.vo.RoleCache;

import org.team4.util.CacheUtil;
import org.team4.util.LoginBean;
import org.team4.util.Validate;

/**
 * 权限工具类,用以获得各种权限信息
 * 
 * @author MarkerKing
 *
 */
public class Meta {

    /**
     * 获得操作权限
     * 
     * @param webId
     * @return
     */
    public static String getOperate(String webId) {
        RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, LoginBean.getLoginBean().getUser().getId());
        HashMap<String, String> operate = roleCache.getOperate();
        if (operate != null) {
            String useable = operate.get(webId);
            if (Validate.notNull(useable))
                return useable;
            else
                return StringConstant.FALSE;
        } else
            return StringConstant.FALSE;
    }

    /**
     * 获得数据范围内记录的操作权限
     * 
     * @param usersId
     * @param webId
     * @return
     */
    public static String getOtherOperate(String usersId, String webId) {
        RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, LoginBean.getLoginBean().getUser().getId());
        HashMap<String, HashMap<String, String>> operate = roleCache.getOtherOperates();
        if (operate != null) {
            HashMap<String, String> usersOperates = operate.get(usersId);
            if (usersOperates != null) {
                String useable = usersOperates.get(webId);
                if (Validate.notNull(useable))
                    return useable;
                else
                    return StringConstant.FALSE;
            } else
                return StringConstant.FALSE;
        } else
            return StringConstant.FALSE;
    }

    /**
     * 获得数据范围ID
     * 
     * @param moduleId
     * @return
     */
    public static String getUsers(String moduleId) {
        RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, LoginBean.getLoginBean().getUser().getId());
        StringBuffer sb = new StringBuffer();
        HashMap<String, String> users = roleCache.getModuleUsers();
        if (users != null) {
            String userIds = users.get(moduleId);
            if (Validate.notNull(userIds)) {
                String[] spId = userIds.split(",");
                for (String id : spId) {
                	if (Validate.notNull(id) && !LoginBean.getLoginBean().getUser().getId().equals(id)) {
                        sb.append("'");
                        sb.append(id);
                        sb.append("',");
                    }
                }
            }
        }

        sb.append("'");
        sb.append(LoginBean.getLoginBean().getUser().getId());
        sb.append("'");

        return sb.toString();
    }
    
    /**
     * 加载部门权限的ID集合
     * @param moduleId
     * @return
     */
    public static String getDepartments(String moduleId) {
    	RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, LoginBean.getLoginBean().getUser().getId());
        HashMap<String, String> departments = roleCache.getModuleDepartments();
        StringBuffer sb = new StringBuffer();
        if(departments != null) {
        	String departmentIds = departments.get(moduleId);
        	if(Validate.notNull(departmentIds)) {
        		String[] spId = departmentIds.split(",");
        		for (String id : spId) {
        			sb.append("'");
        			sb.append(id);
        			sb.append("',");
				}
        	}
        }
        sb.append("'");
        sb.append(LoginBean.getLoginBean().getUser().getDepartment().getId());
        sb.append("'");
        return sb.toString();
    }

    /**
     * 获得字段验证规则
     * @param webId
     * @return
     */
    public static String getRule(String webId) {
        RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, LoginBean.getLoginBean().getUser().getId());
        HashMap<String, String> fieldRule = roleCache.getModuleFields();
        if (fieldRule != null) {
            String rules = fieldRule.get(webId);
            if (Validate.notNull(rules))
                return rules;
            else
                return "[]";
        } else
            return "[]";
    }
}